package com.ksyun.seckill.constants;


import java.text.MessageFormat;

/**
 * 武汉工程大学（Wuhan Institute of Technology），简称工程大（WIT)
 * 可读性和可管理性
 * 以业务名(或数据库名)为前缀(防止key冲突)，用冒号分隔，比如业务名:表名:id
 *
 * @author ksc
 */
public enum RedisKey {
    /**
     * 活动上线时，要写的key.
     * <p>
     * hash结构：
     * 是否初始化:init，存储的是活动的结束时间
     * 库存:stock
     */
    CAMPAIGN_INFO("CAMPAIGN_INFO", "wit:sc", "c:{0}"),

    /**
     * 已参与抢购的客户列表.
     */
    HAVE_ORDER_USER_LIST("HAVE_ORDER_USER_LIST", "wit:sc", "ul:{0}"),

    /**
     * 验证码key.
     * 0:活动id  1:手机号
     */
    VERIFY_CODE("VERIFY_CODE", "wit:sc", "vc:{0}:{1}"),

    /**
     * 秒杀路径key
     * 0:活动id  1:手机号
     */
    SEC_KILL_PATH("SEC_KILL_PATH", "wit:sc", "p:{0}:{1}");

    private final String keyType;

    //秒杀路径的key 的ttl为30l  建议为分钟
    public static final Long SEC_KILL_PATH_TTL = 30L;

    //消息队列的key
    public static final String STREAMS_ORDER_KEY="stream:orders";

    private final String keyPrefix;

    private final String keyFormat;

    RedisKey(String keyType, String keyPrefix, String keyFormat) {
        this.keyType = keyType;
        this.keyPrefix = keyPrefix;
        this.keyFormat = keyFormat;
    }

    public String formatKey(Object... arguments) {
        String key = MessageFormat.format(keyFormat, arguments);
        return keyPrefix + ":" + key;
    }
}
